package com.yuanfeng.activity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.activity.dto.PiecingTogetherConfigDTO;
import com.yuanfeng.activity.dto.WebPinTuanParamDTO;
import com.yuanfeng.activity.entity.ActivityPiecingTogetherConfigEntity;
import com.yuanfeng.activity.feign.BaseServiceClient;
import com.yuanfeng.activity.mapper.ActivityPiecingTogetherConfigMapper;
import com.yuanfeng.activity.mapper.ActivityPiecingTogetherMapper;
import com.yuanfeng.activity.service.ActivityPiecingTogetherConfigService;
import com.yuanfeng.activity.vo.GoodsPinTuanResultVO;
import com.yuanfeng.activity.vo.PinTuanConfigVO;
import com.yuanfeng.commoms.config.YFConfig;
import com.yuanfeng.commoms.dto.goods.NavSortLogoDTO;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.Json.JsonUtils;
import com.yuanfeng.commoms.util.ParseMapUtils;
import com.yuanfeng.commoms.util.RedisUtil;
import com.yuanfeng.commoms.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
public class ActivityPiecingTogetherConfigServiceImpl extends ServiceImpl<ActivityPiecingTogetherConfigMapper, ActivityPiecingTogetherConfigEntity>
    implements ActivityPiecingTogetherConfigService{

    @Autowired
    private ActivityPiecingTogetherMapper activityPiecingTogetherMapper;
    @Autowired
    private BaseServiceClient baseServiceClient;


    @Override
    public ResponseResult h5ConfigModelDelete(PiecingTogetherConfigDTO param) {
        try {
            //设置为模块2  1 代表模块1 （不可新增 删除）  2 代表 模块 2（可新增 删除）
            int temp=this.baseMapper.delete(new LambdaQueryWrapper<ActivityPiecingTogetherConfigEntity>()
                            .eq(ActivityPiecingTogetherConfigEntity::getConfigType,2)
                            .eq(ActivityPiecingTogetherConfigEntity::getId,param.getId()));
            if(temp == 1){
                return ResponseResult.success(BizCodeEnume.OPERATION_SUCCESSFUL);
            }
            return ResponseResult.fail(BizCodeEnume.OPERATION_FAILED);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.fail(e.getMessage());
        }

    }

    @Override
    public ResponseResult h5ConfigModelAdd(PiecingTogetherConfigDTO param) {
        try {
            //设置为模块2  1 代表模块1 （不可新增 删除）  2 代表 模块 2（可新增 删除）
            param.setConfigType(2);
            Integer temp = this.baseMapper.h5ConfigModelAdd(param);
            if(temp == 1){
                return ResponseResult.success(BizCodeEnume.OPERATION_SUCCESSFUL);
            }
            return ResponseResult.fail(BizCodeEnume.OPERATION_FAILED);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.fail(e.getMessage());
        }

    }

    @Override
    public ResponseResult updateH5Config(PiecingTogetherConfigDTO param) {
        try {
            Integer temp = this.baseMapper.updateH5Config(param);
            if(temp == 1){
                return ResponseResult.success(BizCodeEnume.OPERATION_SUCCESSFUL);
            }
            return ResponseResult.fail(BizCodeEnume.OPERATION_FAILED);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.fail(e.getMessage());
        }
    }

    @Override
    public ResponseResult queryH5Config(Map<String, Object> param) {
        try {
            boolean isExistToken = RedisUtil.exists(YFConfig.YF_PLATFORM_TOKEN_USER + param.get("token"));
            if(!isExistToken) {
                return ResponseResult.fail(BizCodeEnume.TOKEN_IS_INVALID);
            }//查询平台h5页面配置
            List<ActivityPiecingTogetherConfigEntity> pinTuanConfigs = this.baseMapper.selectList(new LambdaQueryWrapper<ActivityPiecingTogetherConfigEntity>());
            if(null != pinTuanConfigs && 0 < pinTuanConfigs.size()){
                for (ActivityPiecingTogetherConfigEntity pinTuanConfig : pinTuanConfigs) {
                    //用于存放h5页面商品信息
                    List<GoodsPinTuanResultVO> goodsPinTuanResults = new ArrayList<>();
                    List<String> list = JsonUtils.string2Obj(pinTuanConfig.getConfigData(), List.class, String.class);
                    if(null != list){
                        for (String commonid : list) {
                            //查询拼团商品信息
                            GoodsPinTuanResultVO goodsPinTuanResult = activityPiecingTogetherMapper.queryPinTunGoods(commonid);
                            if(null != goodsPinTuanResult){
                                goodsPinTuanResults.add(goodsPinTuanResult);
                            }
                        }
                        pinTuanConfig.setGoodsPinTuanResultList(goodsPinTuanResults);
                    }
                }
            }
            return ResponseResult.success(pinTuanConfigs);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.fail(e.getMessage());
        }
    }

    @Override
    public ResponseResult queryH5PinTuanIndex(WebPinTuanParamDTO param) {
        Integer shopId = param.getShopId();
        Map<String,Object> map = new HashMap<>();
        try {
            //查询平台h5页面配置
            List<PinTuanConfigVO> pinTuanConfigs = this.baseMapper.queryH5Config();
            if(null != pinTuanConfigs && !pinTuanConfigs.isEmpty()){
                for (PinTuanConfigVO pinTuanConfig : pinTuanConfigs) {
                    //用于存放h5页面商品信息
                    List<GoodsPinTuanResultVO> goodsPinTuanResults = new ArrayList<>();
                    List<String> list = JsonUtils.string2Obj(pinTuanConfig.getConfigData(), List.class, String.class);
                    if(null != list){
                        for (String commonid : list) {
                            //查询拼团商品信息
                            GoodsPinTuanResultVO goodsPinTuanResult = this.baseMapper.queryPinTunGoods(commonid);
                            if (shopId == null ){
                                if(null != goodsPinTuanResult && goodsPinTuanResult.getGoodsType() != 3){
                                    goodsPinTuanResults.add(goodsPinTuanResult);
                                }
                            }else{
                                if(null != goodsPinTuanResult && goodsPinTuanResult.getShopId().equals(shopId)){
                                    goodsPinTuanResults.add(goodsPinTuanResult);
                                }
                            }

                        }
                        pinTuanConfig.setGoodsPinTuanResultList(goodsPinTuanResults);
                    }
                }
            }
            map.put("pinTuanConfigs",pinTuanConfigs);
            //查询轮播图
            NavSortLogoDTO gdbNavSortLogoDTO = new NavSortLogoDTO();
            gdbNavSortLogoDTO.setConfigType("group_slide_picture_h5");
            ResponseResult resp1 = baseServiceClient.quertNavLogo(gdbNavSortLogoDTO);
            List<NavSortLogoDTO> list = (List<NavSortLogoDTO>) resp1.getData();
            map.put("images",list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.success(map);
    }

    @Override
    public ResponseResult queryPinTuanGoodsAll(WebPinTuanParamDTO param) {
        Map<String,Object> resMap = new HashMap<>();
        try {
            //将对象转换为map
            Map data = ParseMapUtils.beanToMap(param);
            //分页查询数据
            Page<GoodsPinTuanResultVO> page = new Page<>(param.getPage(),param.getLimit());
            IPage<GoodsPinTuanResultVO> resultVOIPage = this.baseMapper.queryPinTuanGoodsAll(page,param);
            resMap.put("goodsPinTuanResults",resultVOIPage.getRecords());
            resMap.put("count",resultVOIPage.getTotal());
            return  ResponseResult.success(resMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.success(resMap);
    }
}




